function [Yp] = ccm(L,Y)
n = size(L,1);

cls = unique(Y);
c = size(cls,1);
nump = c*(c-1)/2;

sz = zeros(c,1);
idx = cell(c,1);
for i = 1:c
    idx{i}=find(Y==cls(i));
    sz(i)=size(idx{i},1);
end

ix = cell(c,c);
Yl = cell(c,c);
Yu = cell(c,c);

fai = zeros(n,nump);

disp('Start optimization...');
tic
options = optimoptions('quadprog','Algorithm','trust-region-reflective');
ct = 1;
for i = 1:c
    for j = i+1:c
        disp(['Training pair ',int2str(i),' vs ',int2str(j)]);
        ix{i,j} = [idx{i};idx{j}];
        Yl{i,j} = -inf*ones(n,1);
        Yl{i,j}(idx{i}) = ones(sz(i),1);
        Yu{i,j} = inf*ones(n,1);
        Yu{i,j}(idx{j}) = -ones(sz(j),1);
        x0 = zeros(n,1);
        x0(idx{i}) = ones(sz(i),1);
        x0(idx{j}) = -ones(sz(j),1);
        fai(:,ct) = quadprog(L,[],[],[],[],[],Yl{i,j},Yu{i,j},x0,options);
        ct = ct+1;
    end
end

vot = zeros(n,c);

ct = 1;
for i = 1:c
    for j = i+1:c
        vot(:,i) = vot(:,i)+(fai(:,ct)>0);
        vot(:,j) = vot(:,j)+(fai(:,ct)<0);
        ct=ct+1;
    end
end
[~,Yp] = max(vot,[],2);
Yp = Yp-1;
end